//
//  CourseTableViewCell.m
//  SoocQiPei
//
//  Created by 段帅 on 2017/4/14.
//  Copyright © 2017年 Sooc. All rights reserved.
//

#import "CourseTableViewCell_qipei.h"
#import "dangXiaoHeader_tool.h"

#define Color_f53129_05 [UIColor colorWithRed:245/255.0 green:49/255.0 blue:41/255.0 alpha:0.8]
#define GREEN [UIColor colorWithRed:42/255.0 green:162/255.0 blue:122/255.0 alpha:0.8]
#define COLOR(R, G, B, A)  [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]

@interface CourseTableViewCell_qipei ()

{
    UIImageView *_icon;
    UIImageView *_secutyIcon;
    UILabel *_title;
    
    UILabel *_person;
    UILabel *_detail;
    UILabel *_learn_num;
    UILabel *_learn_time;
    UIButton *_button;
    
    
    UIImageView *_red_back;
    
    
}
@end

@implementation CourseTableViewCell_qipei

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self createUI];
    }
    
    return self;
}
-(void)createUI{
    
    _icon = [[UIImageView alloc]initWithFrame:CGRectMake(15, 14, 140, 80)];
    [self.contentView addSubview:_icon];
    
    _secutyIcon = [[UIImageView alloc]initWithFrame:CGRectMake(101, 0, 39, 34)];
    _secutyIcon.image = [UIImage imageNamed:@"加密课程"];
    _secutyIcon.hidden = YES;
    //[_icon addSubview:_secutyIcon];
    
    _title = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_icon.frame)+11, 14, SIZEWIDTH-41-140, 0)];
    _title.font = NormalFont(28);
    _title.textColor = Color_title_black_87;
    _title.numberOfLines = 2;
    [self.contentView addSubview:_title];
    
    _person = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_icon.frame)+11, 55, 0, 13.5)];
    _person.font = NormalFont(22);
    _person.textColor = Color_title_black_64;
    [self.contentView addSubview:_person];
    
    _detail = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_person.frame)+6, 55, SIZEWIDTH-190-_person.frame.size.width, 13.5)];
    _detail.font = NormalFont(22);
    _detail.textColor = Color_title_black_64;
    _detail.textAlignment = NSTextAlignmentLeft;
      [self.contentView addSubview:_detail];
    
    
    _learn_num = [[UILabel alloc]initWithFrame:CGRectMake(235, CGRectGetMaxY(_person.frame)+12, SIZEWIDTH - 250, 13.5)];
    _learn_num.font = NormalFont(22);
    _learn_num.textColor = Color_title_black_64;
    _learn_num.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:_learn_num];
    
    
    _red_back = [[UIImageView alloc]initWithImage:[self imageWithColor:[UIColor colorWithWhite:0 alpha:0.5]]];
    [_icon addSubview:_red_back];
    
    _learn_time = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,0, 21)];
    _learn_time.font = NormalFont(22);
    _learn_time.textColor = Color_title_white;
    
    _learn_time.textAlignment = NSTextAlignmentCenter;
    [_red_back addSubview:_learn_time];
    
    
    
    
    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [_button setTitle:@"评价" forState:UIControlStateNormal];
    [_button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    
    [_button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_button];
    
    
    // _button.layer.borderColor = [UIColor redColor].CGColor;
    //_button.layer.borderWidth = 0.5;
    _button.layer.cornerRadius = 3;
    _button.layer.masksToBounds = YES;
    _button.hidden = YES;
    
    _button.titleLabel.font = [UIFont systemFontOfSize:11];
    
    
}

#pragma mark - ------首页  搜索 分类专题  
-(void)setModel:(CourseModel_qipei *)model{
    //首页   搜索  
    
    _model = model;
    
    [_icon sd_setImageWithURL:[NSURL URLWithString:model.kcfm] placeholderImage:[SCToolManager imageWithName:@"占位图-课程" InBundle:@"ToolImage" className:[self class]] options:SDWebImageProgressiveDownload];
    

    _title.text = model.kcmc;
    _title.frame =CGRectMake(CGRectGetMaxX(_icon.frame)+11, 14, SIZEWIDTH-41-140, 0);
    
    [_title sizeToFit];
    //    model.zjls = @"是打飞机爱的色放";
    
    _person.frame = CGRectMake(CGRectGetMaxX(_icon.frame)+11, 55,  SIZEWIDTH-41-140, 13.5);
    
    if (model.zjls.length>4) {
        
        _person.text = [NSString stringWithFormat:@"%@   %@",[model.zjls substringToIndex:4],model.jj];
        
//        _person.text = [model.zjls substringToIndex:4];
        
    }else{
//        _person.text = model.zjls;
        
        _person.text = [NSString stringWithFormat:@"%@   %@",model.zjls,model.jj];

    }

    
    
    _learn_num.text = [NSString stringWithFormat:@"%@人在学",model.kcrs];
    
    
    for (int h = 0; h<5; h++) {
        UIImageView *star1 = (UIImageView *)[self.contentView viewWithTag:100+h];
        UIImageView *star2 = (UIImageView *)[self.contentView viewWithTag:200+h];
        [star1 removeFromSuperview];
        [star2 removeFromSuperview];
    }
    
    
    for (int i = 0; i<5; i++) {
        
        UIImageView *image_star = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_icon.frame)+11+i*12, CGRectGetMaxY(_person.frame)+11, 12, 12)];
        image_star.image = [SCToolManager imageWithName:@"评价-课程" InBundle:@"ToolImage" className:[self class]];
        image_star.tag = 100+i;
        [self.contentView addSubview:image_star];
        
    }
    for (int j = 0; j< [model.kcpj intValue]; j++) {
        
        UIImageView *image_star = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_icon.frame)+11+j*12, CGRectGetMaxY(_person.frame)+11, 12, 12)];
        image_star.image = [SCToolManager imageWithName:@"评价-课程-pass" InBundle:@"ToolImage" className:[self class]];
        image_star.tag = 200+j;
        
        [self.contentView addSubview:image_star];
        
    }
    
    if ([model.isEncrypt intValue]==0) {
        _secutyIcon.hidden = YES;
    }else{
        _secutyIcon.hidden = NO;
        
    }
    
    
    _detail.textColor = [UIColor whiteColor];
    if ([model.is_charge integerValue]==1) {
        //收费
        _detail.text = [NSString stringWithFormat:@"  ￥%@   ",model.price];
    }else{
        _detail.text = @"   免费   ";
    }
    
    CGSize labSize=[self boundingRectWithSize:CGSizeMake(0, 20) andLabel:_detail];
    
    _detail.frame = CGRectMake(15, 14, labSize.width, 20);
    if ([model.is_charge integerValue]==1) {
        //收费
        _detail.backgroundColor = COLOR(245, 65, 58, 1);
    }else{
        _detail.backgroundColor = COLOR(70, 177, 124, 1);
    }

    
    
    if ([MESSAGE_CENTER.isMoney integerValue]==1) {
        //平台收费
        _detail.hidden = NO;
    }else{
        //不收费
        _detail.hidden = YES;
         _person.frame = CGRectMake(CGRectGetMaxX(_icon.frame)+11, CGRectGetMaxY(_icon.frame)-14, _person.frame.size.width,  _person.frame.size.height);
    }
    
    
    
    
}

#pragma mark - ------ 我的收藏

-(void)setModel_collect:(CourseModel_qipei *)model_collect{
    
    _model_collect = model_collect;
    
    [_icon sd_setImageWithURL:[NSURL URLWithString:_model_collect.kcfm] placeholderImage:[SCToolManager imageWithName:@"占位图-课程" InBundle:@"ToolImage" className:[self class]] options:SDWebImageProgressiveDownload];
    
    _title.text = _model_collect.kcmc;
    _title.frame =CGRectMake(CGRectGetMaxX(_icon.frame)+11, 14, SIZEWIDTH-41-140, 0);
    
    [_title sizeToFit];
    //    model.zjls = @"是打飞机爱的色放";
    
    _person.frame = CGRectMake(CGRectGetMaxX(_icon.frame)+11, 55, 0, 13.5);
    _person.text = _model_collect.zjls ;
    [_person sizeToFit];
    
    _learn_num.text = [NSString stringWithFormat:@"%@人在学",_model_collect.kcrs];
    
    
    for (int h = 0; h<5; h++) {
        UIImageView *star1 = (UIImageView *)[self.contentView viewWithTag:100+h];
        UIImageView *star2 = (UIImageView *)[self.contentView viewWithTag:200+h];
        [star1 removeFromSuperview];
        [star2 removeFromSuperview];
    }
    
    
    _detail.frame = CGRectMake(CGRectGetMaxX(_icon.frame)+11, CGRectGetMaxY(_person.frame)+12, 0, 13.5);
    if ([model_collect.is_charge integerValue]==1) {
        //收费
        _detail.text = [NSString stringWithFormat:@"￥%@",model_collect.price];
        _detail.textColor = [UIColor redColor];
    }else{
        _detail.text = @"免费";
        _detail.textColor = GREEN;
    }
    
    [_detail sizeToFit];
    
    if ([MESSAGE_CENTER.isMoney integerValue]==1) {
        //平台收费
        _detail.hidden = NO;
        _learn_num.hidden = YES;
    }else{
        //不收费
          _learn_num.hidden = NO;
        _detail.hidden = YES;
          _person.frame = CGRectMake(CGRectGetMaxX(_icon.frame)+11, CGRectGetMaxY(_icon.frame)-14, _person.frame.size.width,  _person.frame.size.height);
        
    }
    
    _secutyIcon.hidden = YES;
    _red_back.hidden = YES;
    _learn_time.hidden = YES;
    
}


#pragma mark - ------我的课程  正在学习
-(void)setModel_not_finish:(CourseModel_qipei *)model_not_finish{
    
    
    
    _model_not_finish = model_not_finish;
    
    [_icon sd_setImageWithURL:[NSURL URLWithString:model_not_finish.kcfm] placeholderImage:[SCToolManager imageWithName:@"占位图-课程" InBundle:@"ToolImage" className:[self class]] options:SDWebImageProgressiveDownload];
    
    _learn_time.text = [NSString stringWithFormat:@"已学习%@/%@课",model_not_finish.kcjd,model_not_finish.kczs];
    [_learn_time sizeToFit];
    _learn_time.frame = CGRectMake( 5, 0, _learn_time.frame.size.width, 21);
    _red_back.frame = CGRectMake(_icon.frame.size.width-10-_learn_time.frame.size.width, _icon.frame.size.height-21,_learn_time.frame.size.width+10, 21);
    _title.text = model_not_finish.kcmc;
    _title.frame =CGRectMake(CGRectGetMaxX(_icon.frame)+11, 14, SIZEWIDTH-41-140, 0);
    [_title sizeToFit];
    
    _learn_num.frame = CGRectMake(CGRectGetMaxX(_icon.frame)+11, 55, SIZEWIDTH-200, 13.5);
    _learn_num.textAlignment = NSTextAlignmentLeft;
    
    if (model_not_finish.update_time.length == 0 || model_not_finish.update_time == nil) {
         _learn_num.hidden = YES;
    }else{
        _learn_num.hidden = NO;
          _learn_num.text = [NSString stringWithFormat:@"上次学习 %@", [self since1970String:model_not_finish.update_time]];
    }

    
  
    
    _person.frame = CGRectMake(CGRectGetMaxX(_icon.frame)+11, CGRectGetMaxY(_learn_num.frame)+12, 0, 13.5);
    _person.text = model_not_finish.kcjs ;
    [_person sizeToFit];
        
      
    
    _button.frame = CGRectMake(SIZEWIDTH -50 , CGRectGetMinY(_person.frame)-5, 50, 23);
    
    
    
    if ([model_not_finish.isJudge integerValue]>0) {
        //已评价
        _button.hidden = YES;
    }else{
        _button.hidden = NO;
    }
    
      
   
    
    
    
    
       
}
#pragma mark - ------我的课程  学习完成
-(void)setModel_finish:(CourseModel_qipei *)model_finish{
    _model_finish = model_finish;
    
    [_icon sd_setImageWithURL:[NSURL URLWithString:model_finish.kcfm] placeholderImage:[SCToolManager imageWithName:@"占位图-课程" InBundle:@"ToolImage" className:[self class]] options:SDWebImageProgressiveDownload];
    
  
 
    _title.text = model_finish.kcmc;
    _title.frame =CGRectMake(CGRectGetMaxX(_icon.frame)+11, 14, SIZEWIDTH-41-140, 0);
    [_title sizeToFit];
    
   
        _person.text = model_finish.kcjs;
        [_person sizeToFit];
        
      
    
    
    
    
    
    
    
    
    _button.frame = CGRectMake(SIZEWIDTH -50 , CGRectGetMaxY(_person.frame)+5, 50, 23);
    
    
    
    if ([model_finish.isJudge integerValue]>0) {
        //已评价
        _button.hidden = YES;
    }else{
        _button.hidden = NO;
    }

    
      
    
    
    
    
    for (int h = 0; h<5; h++) {
        UIImageView *star1 = (UIImageView *)[self.contentView viewWithTag:100+h];
        UIImageView *star2 = (UIImageView *)[self.contentView viewWithTag:200+h];
        [star1 removeFromSuperview];
        [star2 removeFromSuperview];
    }
    
    
    for (int i = 0; i<5; i++) {
        
        UIImageView *image_star = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_icon.frame)+11+i*12, CGRectGetMaxY(_person.frame)+12, 12, 12)];
        image_star.image = [SCToolManager imageWithName:@"评价-课程" InBundle:@"ToolImage" className:[self class]];
        image_star.tag = 100+i;
        [self.contentView addSubview:image_star];
        
    }
    for (int j = 0; j< [model_finish.kcpj intValue]; j++) {
        
        UIImageView *image_star = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_icon.frame)+11+j*12, CGRectGetMaxY(_person.frame)+12, 12, 12)];
        image_star.image = [SCToolManager imageWithName:@"评价-课程-pass" InBundle:@"ToolImage" className:[self class]];
        image_star.tag = 200+j;
        
        [self.contentView addSubview:image_star];
        
    }
    
    
    
    
    
    
    
}


-(void)clickButton:(UIButton *)button{
    //点击评价
    
    if (self.judgeScroe != NULL) {
        
        if (_model_finish) {
            self.judgeScroe(_model_finish,button);
        }else{
            self.judgeScroe(_model_not_finish,button);
            
        }
        
    }
    
    
    
    
}
#pragma mark - ------我的笔记
-(void)setModel_note:(MineNoteModel *)model_note{
    
    _model_note = model_note;
    
    [_icon sd_setImageWithURL:[NSURL URLWithString:model_note.kcfm] placeholderImage:[SCToolManager imageWithName:@"占位图-课程" InBundle:@"ToolImage" className:[self class]] options:SDWebImageProgressiveDownload];
    
    _learn_time.hidden = YES;    
    _title.text = model_note.kcmc;
    _title.frame =CGRectMake(CGRectGetMaxX(_icon.frame)+11, 14, SIZEWIDTH-41-140, 0);
    [_title sizeToFit];
    
    
    _person.hidden = YES;
    
    _detail.hidden = YES;
    
    _learn_num.frame = CGRectMake(CGRectGetMaxX(_icon.frame)+11, CGRectGetMaxY(_icon.frame)-13.5, SIZEWIDTH-200, 13.5);
    _learn_num.textAlignment = NSTextAlignmentLeft;
    _learn_num.text = [NSString stringWithFormat:@"笔记数: %@",model_note.bjsl];
    
    _secutyIcon.hidden = YES;
    
}

//时间戳转换
- (NSString *)since1970String:(NSString *)data
{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd "];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[data intValue]];
    NSString * nowtimeStr = [formatter stringFromDate:date];
    
    return nowtimeStr;
}

- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}
- (CGSize)boundingRectWithSize:(CGSize)size andLabel:(UILabel *)lab
{
    NSDictionary *attribute = @{NSFontAttributeName:NormalFont(22)};
    lab.numberOfLines=0;
    CGSize retSize = [lab.text boundingRectWithSize:size
                                             options:\
                      NSStringDrawingTruncatesLastVisibleLine |
                      NSStringDrawingUsesLineFragmentOrigin |
                      NSStringDrawingUsesFontLeading
                                          attributes:attribute
                                             context:nil].size;
    
    return retSize;
}
@end

